package org.springframework.aop;

import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;

public class SingletonTargetSource implements TargetSource, Serializable
{
    /**
     * use serialVersionUID from Spring 1.2 for interoperability
     */
    private static final long serialVersionUID = 9031246629662423738L;
    /**
     * Target cached and invoked using reflection
     */
    private final Object target;

    /**
     * Create a new SingletonTargetSource for the given target.
     *
     * @param target the target object
     */
    public SingletonTargetSource(Object target)
    {
        Assert.notNull(target, "Target object must not be null");
        this.target = target;
    }

    public Class getTargetClass()
    {
        return this.target.getClass();
    }

    public Object getTarget()
    {
        return this.target;
    }

    public void releaseTarget(Object target)
    {
        // nothing to do
    }

    public boolean isStatic()
    {
        return true;
    }

    /**
     * Two invoker interceptors are equal if they have the same target or if the
     * targets or the targets are equal.
     */
    public boolean equals(Object other)
    {
        if (this == other)
        {
            return true;
        }
        if (!(other instanceof SingletonTargetSource))
        {
            return false;
        }
        SingletonTargetSource otherTargetSource = (SingletonTargetSource) other;
        return this.target.equals(otherTargetSource.target);
    }

    /**
     * SingletonTargetSource uses the hash code of the target object.
     */
    public int hashCode()
    {
        return this.target.hashCode();
    }

    public String toString()
    {
        return "SingletonTargetSource for target object [" + ObjectUtils.identityToString(this.target) + "]";
    }
}
